#region Using directives

using System;
using System.Text ;
using System.Data ;
using System.Collections ;

using WP.MF.DataEntity; 
using WP.MF.DataAccess; 
using WP.MF.BusinessRule;
using WP.Commons;
using WP.DB;

#endregion
namespace WP.MF.BusinessLogical
{

    public class MMenuBLL : IBusinessLogical
    {
        string strSql="";
        MMenuDAL mMenuDAL =new MMenuDAL ();

        public MMenuBLL()
        {
        }

        #region For implement interface

        #region Select Record by RECID OverLoad +1
        public MMenuDEL SelectDataByRecID(DBAccess dBAccess, string strRecId)
        {
            MMenuDEL mMenuDEL = new MMenuDEL();

            DataFunctions.SelectByRecID(dBAccess, mMenuDEL, mMenuDAL.Fields, strRecId);

            return mMenuDEL;
        }
        public IDataEntity SelectDataByRecID(string strRecId)
        {
            DBAccess dBAccess = new DBAccess(false, false);
            MMenuDEL mMenuDEL = null;

            try
            {
                mMenuDEL = SelectDataByRecID(dBAccess, strRecId);
                return mMenuDEL as IDataEntity;
            }
            catch (Exception e)
            {
                ExceptionManager.ProcessException(e);
                return null;
            }
            finally
            {
                dBAccess.CloseCon();
            }
        }
        #endregion

		#region Insert MMenu
        public bool Insert(IDataEntity iMMenuDEL)
		{
            bool bReturn = false;
            MMenuDEL mMenuDEL = iMMenuDEL as MMenuDEL;
			DBAccess dBAccess=new DBAccess(false,true);

            try
            {
                if (new MMenuBRL().InsertMenuValid(dBAccess, mMenuDEL))
                {
                    mMenuDAL.InsertMMenu(dBAccess, mMenuDEL);
                    dBAccess.CommitTranAndCloseCon();
                    bReturn = true;
                    ReflectMethod.frmMainMenu_RefreshMenu();  
                }
                else
                {
                    dBAccess.RollbackTranAndClose();
                    if (SysVar.ErrorMessage.Count > 0)
                    {
                        MsgBoxEx.Show(SysVar.ErrorMessage, SysVar.WarnTitle);
                    }
                }
            }
            catch (Exception e)
            {
                dBAccess.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
            }
            return bReturn;
		}
		#endregion

		#region Update MMenu
        public bool Update(IDataEntity iMMenuDEL)
		{
            bool bReturn = false;
            MMenuDEL mMenuDEL = iMMenuDEL as MMenuDEL;
			DBAccess dBAccess=new DBAccess(false,true);

			try
			{
                if(new MMenuBRL().UpdateMenuValid(dBAccess,mMenuDEL))
			    {
					mMenuDAL.UpdateMMenu(dBAccess,mMenuDEL);
					dBAccess.CommitTranAndCloseCon();
                    ReflectMethod.frmMainMenu_RefreshMenu();
					return true;
				}
			    else
			    {
                    dBAccess.RollbackTranAndClose();
                    if (SysVar.ErrorMessage.Count > 0)
                    {
                        MsgBoxEx.Show(SysVar.ErrorMessage, SysVar.WarnTitle);
                    }
			    }
            }
            catch (Exception e)
			{
                dBAccess.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
			}
            return false;
		}
		#endregion

		#region Delete MMenu
        public bool Delete(string strRecId, byte[] btOldRecStamp)
		{
			string strKey ="";
			ArrayList arList = new ArrayList();
			DBAccess dBAccess=new DBAccess(false,true);
            bool bReturn = false;

			try
			{
                if (new MMenuBRL().DeleteMMenuValid(dBAccess, strRecId, btOldRecStamp))
    			{
				    MMenuDEL mMenuDEL=mMenuDAL.SelectMMenuByRecId(dBAccess,strRecId);	
				    DataRow DR=mMenuDEL.Tables[MMenuDEL.TABLENAME_MMENU].Rows[0];

				    strKey = strKey + DR[MMenuDEL.MENUID].ToString().Trim();
				    if(strKey.Substring(9,3) == "000")
					    strKey = strKey.Substring(0,9);
				    if(strKey.Substring(6,3) == "000")
					    strKey = strKey.Substring(0,6);
				    strKey = strKey + "%";
					
				    arList = mMenuDAL.GetSubMenuByCondition(dBAccess,strKey);

				    //delete sub menu
				    for(int i=0;i<arList.Count;i++)
				    {
					    mMenuDAL.DeleteMMenu(dBAccess,arList[i].ToString());
				    }
				    mMenuDAL.DeleteMMenu(dBAccess,strRecId);

				    dBAccess.CommitTranAndCloseCon();
                    bReturn = true;
			    }
		        else
		        {
                    dBAccess.RollbackTranAndClose();
                    if (SysVar.ErrorMessage.Count > 0)
                    {
                        MsgBoxEx.Show(SysVar.ErrorMessage, SysVar.WarnTitle);
                    }
		        }
            }
		    catch(Exception e)
		    {
			    dBAccess.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
            }

            return bReturn;
        }
		#endregion
        #endregion


        #region Select MMenu
        /// <summary>
        /// get menu by menuID
        /// </summary>
        /// <param name="MenuID"></param>
        /// <returns></returns>
        public MMenuDEL SelectMMenuByKey(string MenuID)
        {
            MMenuDEL mMenuDEL = new MMenuDEL();
            DBAccess dBAccess = new DBAccess();
            return mMenuDAL.SelectMMenuByKey(dBAccess, MenuID);
        }
        /// <summary>
        /// get menu by RecID
        /// </summary>
        /// <param name="MenuID"></param>
        /// <returns></returns>
        public MMenuDEL SelectMMenuByRecId(string RecID)
        {
            MMenuDEL mMenuDEL = new MMenuDEL();
            DBAccess dBAccess = new DBAccess();
            return mMenuDAL.SelectMMenuByRecId(dBAccess, RecID);
        }

        /// <summary>
        /// get menu by menuName
        /// </summary>
        /// <param name="MenuName"></param>
        /// <returns></returns>
        public MMenuDEL SelectMMenuByMenuName(string MenuName)
        {
            MMenuDEL mMenuDEL = new MMenuDEL();
            DBAccess dBAccess = new DBAccess();
            return mMenuDAL.SelectMMenuByMenuName(dBAccess, MenuName);
        }

        /// <summary>
        /// get menu by condition
        /// </summary>
        /// <param name="MenuID"></param>
        /// <param name="strConditon1"></param>
        /// <param name="MenuName"></param>
        /// <param name="strCondition2"></param>
        /// <returns>dataset</returns>
        public MMenuDEL SelectMMenuByCondition(string MenuID, string strConditon1, string MenuName, string strCondition2)
        {
            MMenuDEL mMenuDEL = new MMenuDEL();
            DBAccess dBAccess = new DBAccess();
            return mMenuDAL.SelectMMenuByCondition(dBAccess, MenuID, strConditon1, MenuName, strCondition2);
        }
        /// <summary>
        /// get menu by condition for paging
        /// </summary>
        /// <param name="MenuID"></param>
        /// <param name="strConditon1"></param>
        /// <param name="MenuName"></param>
        /// <param name="strCondition2"></param>
        /// <returns>string</returns>
        public string SelectMMenuByConditionForPage(string MenuID, string strConditon1, string MenuName, string strCondition2)
        {
            MMenuDEL mMenuDEL = new MMenuDEL();
            DBAccess dBAccess = new DBAccess();
            return mMenuDAL.SelectMMenuByConditionForPage(dBAccess, MenuID, strConditon1, MenuName, strCondition2);
        }

        #endregion

        #region Get Menus for menu tree
        public MMenuDEL GetMMenuForTree()
        {
            MMenuDEL mMenuDE=null;
            string strGroup="";
            string strPermit="";
            string strTraderPrompt="";
            DBAccess dBAccess=new DBAccess (false,false);
            MUserDAL mUserDAL=new MUserDAL();
            MUserDEL mUserDEL=new MUserDEL();

            try
            {
                strSql=mUserDAL.SelectForMenuTree();

                dBAccess.GetDataSet(strSql,"mUser",mUserDEL);

                if (mUserDEL.Tables[MUserDEL.TABLENAME_MUSER].Rows.Count!=0)
                {
                   strGroup=mUserDAL.GetGroupFieldNameByModule();
                   if(int.Parse(mUserDEL.Tables[MUserDEL.TABLENAME_MUSER].Rows[0][strGroup].ToString().Trim())<10)
                   {
                      strPermit="permit0" + mUserDEL.Tables[MUserDEL.TABLENAME_MUSER].Rows[0][strGroup].ToString().Trim();               
                   }
                   else
                   {
                      strPermit="permit" + mUserDEL.Tables[MUserDEL.TABLENAME_MUSER].Rows[0][strGroup].ToString().Trim();
                   }

                   strSql=mMenuDAL.SelectmMenuForMenuTree(strPermit);

                   mMenuDE=new MMenuDEL ();
                   dBAccess.GetDataSet(strSql,MMenuDEL.TABLENAME_MMENU,mMenuDE);

                   if (mMenuDE.Tables[MMenuDEL.TABLENAME_MMENU].Rows.Count>0)
                   {
                      strTraderPrompt= SysVar.TraderPrompt;
                      if (strTraderPrompt!="" && strTraderPrompt!="Trader")
                      {       
                         foreach(DataRow DR in mMenuDE.Tables[MMenuDEL.TABLENAME_MMENU].Rows)
                         {
                            DR[MMenuDEL.MENUNAME]=DR[MMenuDEL.MENUNAME].ToString().Replace("Trader",strTraderPrompt);
                         }
                      }
                   }
                }
                dBAccess.CloseCon();
                return mMenuDE;
            }
            catch(Exception e)
            {
                dBAccess.CloseCon();
                ExceptionManager.ProcessException(new BaseException(e.Message));
                return null;
            }
        }
        #endregion

    }
}